# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-TODAY OpenERP S.A. <http://www.openerp.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import copy
from datetime import datetime
from time import strftime

from openerp.osv import fields, osv

class zscloud_dept(osv.osv):
    _name = 'zscloud.dept'
    _description = '使用云资源的部门或公司'
    _rec_name = 'name'
    _columns = {
        'name': fields.char("名称", size=128, required=1),
        'type': fields.selection([(u'政府机关', u'政府机关'), (u'事业单位', u'事业单位'),(u'公司', u'公司') ], "类型"),
        'bz': fields.text("备注"),
        'lxr_ids': fields.one2many('zscloud.lxr', 'dept_id', '联系人'),
        'zysqd_ids': fields.one2many('zscloud.zysqd', 'dept_id', '资源申请单'),
    }
zscloud_dept()

class zscloud_lxr(osv.osv):
    _name = 'zscloud.lxr'
    _description = '工作联系人'
    _rec_name = 'name'


    _columns = {
        'name': fields.char("姓名", size=10, required=1),
        'title': fields.char('职位', size=20),
        'email': fields.char('Email', size=240),
        'phone': fields.char('电话', size=64),
        'fax': fields.char('传真', size=64),
        'mobile': fields.char('手机', size=64),
        'dept_id': fields.many2one('zscloud.dept', '所属部门或公司'),
    }
zscloud_lxr()

class zscloud_zysqd(osv.osv):
    _name = 'zscloud.zysqd'
    _description = '资源申请单'
    _rec_name = 'sqmd'
    _columns = {
        'sqmd': fields.char('申请目的',),
        'dept_id': fields.many2one('zscloud.dept', '申请单位'),
        'dept_lxr_id': fields.many2one('zscloud.lxr', '部门联系人'),
        'yw_lxr_id': fields.many2one('zscloud.lxr', '运维公司联系人'),
        'state': fields.selection([(u'审批中',u'审批中'),(u'资源分配中',u'资源分配中'),(u'部署中',u'部署中'),(u'试运行中',u'试运行中'),(u'已上线',u'已上线')],'状态'),
        'user_id': fields.many2one('res.users','负责人'),
        'sq_date': fields.date('申请日期'),
        'fp_date': fields.date('分配日期'),
        'bs_date': fields.date('部署日期'),
        'sx_date': fields.date('上线日期'),
        'bz': fields.text("备注"),
        'line_ids': fields.one2many('zscloud.zysqd.line', 'zysqd_id', '资源明细'),
        'attachment_ids': fields.many2many('ir.attachment', 'zscloud_zysqd_attachment_rel','zysqd_id', 'attachment_id', '附件'),
    }
    _defaults = {
         'sq_date': fields.datetime.now()
    }
zscloud_zysqd()

class zscloud_apps(osv.osv):
    _name = 'zscloud.apps'
    _description = '应用系统'
    _rec_name = 'name'

    _columns = {
        'name': fields.char("名称", size=50, required=1),
        'dept_id': fields.many2one('zscloud.dept', '所属部门或公司'),
        'yw_dept_id': fields.many2one('zscloud.dept', '运维公司'),
        'line_ids': fields.one2many('zscloud.zysqd.line','app_id', '使用的资源'),
        'url': fields.char('外网访地址',size=50),
        'inurl1': fields.char('云平台地址1',size=50),
        'inurl2': fields.char('云平台地址2',size=50),
        'inurl3': fields.char('云平台地址3',size=50),
    }
zscloud_apps()

class zscloud_zysqd_line(osv.osv):
    _name = 'zscloud.zysqd.line'
    _description = '资源申请明细'
    _rec_name = 'yt'
    _columns = {
        'yt': fields.char('用途'),
        'zysqd_id': fields.many2one('zscloud.zysqd', '资源申请单'),
        'sq_yzjlx': fields.selection([('A','A'),('B','B'),('C','C'),('D','D'),('E','E'),('F','F')],'申请的云主机类型'),
        'sq_vcpu': fields.integer('申请的VCPU'),
        'sq_memory': fields.integer('申请的内存'),
        'sq_sys_disk': fields.integer('申请的系统盘'),
        'sq_data_disk': fields.integer('申请的数据盘'),
        'sq_bak_disk': fields.integer('申请的备份盘'),
        'fp_yzjlx': fields.selection([('A','A'),('B','B'),('C','C'),('D','D'),('E','E'),('F','F')],'分配的云主机类型'),
        'fp_vcpu': fields.integer('分配的VCPU'),
        'fp_memory': fields.integer('分配的内存'),
        'fp_sys_disk': fields.integer('分配的系统盘'),
        'fp_data_disk': fields.integer('分配的数据盘'),
        'fp_bak_disk': fields.integer('分配的备份盘'),
        'ip1': fields.char('IP_1',size=15),
        'ip2': fields.char('IP_2',size=15),
        'ip3': fields.char('IP_3',size=15),
        'bz': fields.text("备注"),
        'os': fields.char("操作系统",size=128),
        'zjj': fields.char("中间件",size=128),
        'app_id': fields.many2one('zscloud.apps', '应用系统'),
    }
    
    
    def on_change_fp_yzjlx(self, cr, uid, ids, fp_yzjlx, context=None):
        val = {}
        
        if fp_yzjlx in ['A']:
            val['fp_vcpu'] = 1
            val['fp_memory'] = 1
            val['fp_sys_disk'] = 80
            return {'value': val}

        elif fp_yzjlx in ['B']:
            val['fp_vcpu'] = 2
            val['fp_memory'] = 2
            val['fp_sys_disk'] = 80
            return {'value': val}

        elif fp_yzjlx in ['C']:
            val['fp_vcpu'] = 2
            val['fp_memory'] = 4
            val['fp_sys_disk'] = 80
            return {'value': val}

        elif fp_yzjlx in ['D']:
            val['fp_vcpu'] = 4
            val['fp_memory'] = 4
            val['fp_sys_disk'] = 100
            return {'value': val}

        elif fp_yzjlx in ['E']:
            val['fp_vcpu'] = 4
            val['fp_memory'] = 8
            val['fp_sys_disk'] = 100
            return {'value': val}
        
        elif fp_yzjlx in ['F']:
            val['fp_vcpu'] = 8
            val['fp_memory'] = 16
            val['fp_sys_disk'] = 100
            return {'value': val}
        
    def on_change_sq_yzjlx(self, cr, uid, ids, sq_yzjlx, context=None):
        val = {}

        if sq_yzjlx in ['A']:
            val['sq_vcpu'] = 1
            val['sq_memory'] = 1
            val['sq_sys_disk'] = 80
            return {'value': val}

        elif sq_yzjlx in ['B']:
            val['sq_vcpu'] = 2
            val['sq_memory'] = 2
            val['sq_sys_disk'] = 80
            return {'value': val}

        elif sq_yzjlx in ['C']:
            val['sq_vcpu'] = 2
            val['sq_memory'] = 4
            val['sq_sys_disk'] = 80
            return {'value': val}

        elif sq_yzjlx in ['D']:
            val['sq_vcpu'] = 4
            val['sq_memory'] = 4
            val['sq_sys_disk'] = 100
            return {'value': val}

        elif sq_yzjlx in ['E']:
            val['sq_vcpu'] = 4
            val['sq_memory'] = 8
            val['sq_sys_disk'] = 100
            return {'value': val}
        
        elif sq_yzjlx in ['F']:
            val['sq_vcpu'] = 8
            val['sq_memory'] = 16
            val['sq_sys_disk'] = 100
            return {'value': val}
        
zscloud_zysqd_line()



# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
